﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
using System.Xml.Linq;
using System.Xml;
using System.Web.Caching;
using System.IO;

namespace System.Helper
{
    public class CacheHelper
    {
        /// <summary>
	    /// 获取数据缓存
	    /// </summary>
	    /// <param name="CacheKey">键</param>
	    public static object GetCache(string CacheKey)
	    {
	        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
	        return objCache[CacheKey];
	    }
	
	    /// <summary>
	    /// 设置数据缓存
	    /// </summary>
	    public static void SetCache(string CacheKey, object objObject)
	    {
	        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
	        objCache.Insert(CacheKey, objObject);
	    }

        /// <summary>
        /// 设置XML文件依赖缓存
        /// </summary>
        public static XDocument GetXMLFileCache(string XMLFilePath)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            XDocument xdoc = new XDocument();
            //objCache.Insert(CacheKey, objObject);
            if (objCache[XMLFilePath] == null)
            {
                try
                {
                    xdoc = XDocument.Load(XMLFilePath);
                    CacheDependency cdy = new CacheDependency(XMLFilePath);
                    objCache.Insert(XMLFilePath, xdoc, cdy);
                }
                catch
                {

                }

            }
            else {
                xdoc = (XDocument)objCache[XMLFilePath];
            }

            return xdoc;
        }

        /// <summary>
        /// 获取数据,如果在缓存中存在,则取缓存中的,如果不在,则取文件中的
        /// </summary>
        /// <param name="path"></param>
        private XmlDocument GetData(string path)
        {
            //文件不存在
            if (!File.Exists(path))
            {
                return null;
            }
            XmlDocument doc = new XmlDocument();

            CacheDependency mydep = new CacheDependency(path);
            if (mydep.HasChanged || System.Web.HttpContext.Current.Cache["XMLDATA"] == null)
            {
                try
                {
                    doc.Load(path);
                    //如果缓存不存在或缓存依赖有变动,则添加——保存时间是3小时
                    System.Web.HttpContext.Current.Cache.Add("XMLDATA", doc, mydep, DateTime.Now.AddSeconds(3 * 60 * 60),
                        TimeSpan.Zero, CacheItemPriority.Normal, null);
                }
                catch
                {
                    throw;
                }
            }
            else
            {
                doc = (XmlDocument)System.Web.HttpContext.Current.Cache["XMLDATA"];
            }
            return doc;
        }


	    /// <summary>
	    /// 设置数据缓存
	    /// </summary>
	    public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
	    {
	        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
	        objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
	    }
	
	    /// <summary>
	    /// 设置数据缓存
	    /// </summary>
	    public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
	    {
	        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
	        objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
	    }
	
	    /// <summary>
	    /// 移除指定数据缓存
	    /// </summary>
	    public static void RemoveAllCache(string CacheKey)
	    {
	        System.Web.Caching.Cache _cache = HttpRuntime.Cache;
	        _cache.Remove(CacheKey);
	    }
	
	    /// <summary>
	    /// 移除全部缓存
	    /// </summary>
	    public static void RemoveAllCache()
	    {
	        System.Web.Caching.Cache _cache = HttpRuntime.Cache;
	        IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
	        while (CacheEnum.MoveNext())
	        {
	            _cache.Remove(CacheEnum.Key.ToString());
	        }
	    }
    }

}
